package me.thinkjet.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.DwzRender;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import me.thinkjet.model.Discuss;
import me.thinkjet.model.Job;
import me.thinkjet.model.News;
import me.thinkjet.service.TagService;
import me.thinkjet.service.UserService;

@ControllerBind(controllerKey = "/discuss", viewPath = "discuss")
public class DiscussController extends Controller {
	public void index() {
		int page = getParaToInt("pageNum", 1);
		int numPerPage = getParaToInt("numPerPage", 20);
		boolean hot = getParaToBoolean("hot", false);
		Page<Discuss> discuss = Discuss.dao.paginate(page, numPerPage, "select d.*, r.*, u.username",
				"from discuss d left join discuss_record r on d.id=r.discuss_id " +
						"left join users u on u.id=d.author" + (hot ? "where hot=1" : ""));
		for (Discuss n : discuss.getList()) {
			n.put("tags", TagService.getDiscussTags(n.getLong("id"), false));
		}
		this.setAttr("discuss", discuss);
	}

	public void edit() {
		setAttr("discuss", Discuss.dao.findById(getPara()));
	}

	public void update() {
		if (getModel(Discuss.class).update()) {
			render(DwzRender.closeCurrentAndRefresh("discuss_edit"));
		} else {
			render(DwzRender.error());
		}
	}

	public void delete() {
		if (Discuss.dao.deleteById(getPara())) {
			render(DwzRender.success());
		} else {
			render(DwzRender.error());
		}
	}

	public void add() {

	}

	public void create() {
		if (getModel(Job.class).save()) {
			render(DwzRender.closeCurrentAndRefresh("discuss_add"));
		} else {
			render(DwzRender.error());
		}
	}


}